import csv
from telnetlib import EC
from selenium.webdriver.support import expected_conditions as EC
from urllib import request

from selenium import webdriver
from selenium.webdriver.common.by import By
import os
import requests
import time

from selenium.webdriver.support.wait import WebDriverWait

options = webdriver.EdgeOptions()  # 创建配置项对象
options.add_argument("--disable-blink-features=AutomationControlled")  # 添加配置项:跳过部分反爬虫程序
browser = webdriver.Edge(options=options)  # 将配置项添加到浏览器控制对象中
browser.get('https://piaofang.maoyan.com/rankings/year')  # 跳转页面
data = []
nt = int
n=0
nian = len(browser.find_elements(By.XPATH, '//*[@id="tab-year"]/ul/li')) - 1
browser.find_element(By.XPATH, '//*[@id="tab-year"]/ul/li[2]').click()
print(nian)
for n in range(1):
    time.sleep(5)
    nums = len(browser.find_elements(By.XPATH, '//*[@id="ranks-list"]/ul'))
    for i in range(nums):
    # 等待元素加载完成
        element = WebDriverWait(browser, 50).until(
                EC.presence_of_element_located((By.XPATH, '//*[@id="ranks-list"]/ul[{}]/li[2]/p[1]'.format(i+1)))
        )
        textname = element.text
        # textname = browser.find_element(By.XPATH,
        #                                 '//*[@id="ranks-list"]/ul['+str(i+1)+']/li[2]/p[1]'.format(
        #                                     i + 1)).text
        element01 = WebDriverWait(browser, 50).until(
            EC.presence_of_element_located((By.XPATH, '//*[@id="ranks-list"]/ul[{}]/li[3]'.format(i+1)))
        )
        textname01 = element.text
        # textinfor01 = browser.find_element(By.XPATH,
        #                                    '//*[@id="ranks-list"]/ul['+str(i+1)+']/li[3]'.format(
        #                                        i + 1)).text
        browser.find_element(By.XPATH, '//*[@id="ranks-list"]/ul[1]').click()
        try:
            # 显式等待，最长等待10秒
            target_element01 = WebDriverWait(browser, 55).until(
                browser.find_element(
                    (By.XPATH, '/html/body/div[2]/section/div/div[2]/div[1]/div[2]/div[2]/div[1]/div/p'.format()))
            ).text
            if '动画' in target_element01:
                result = "动画"
            else:
                result = "其他"
            target_element02 = WebDriverWait(browser, 55).until(
                browser.find_element((By.XPATH,
                                      '/html/body/div[2]/section[1]/div/div[2]/div[1]/div[2]/div[2]/div[1]/div/div[1]/div/p'.format()))
            ).text.split('/')[0]
            if '中国' in target_element01:
                result = "中国"
            else:
                result = "国外"
            data.append([textname, textname01, target_element01, target_element02])
        except Exception as e:
            print(f"元素未找到或超时：{e}")
        finally:
            # 返回到上一个页面
            browser.back()
    nt = n + 1
    ntt=str(nt)
    browser.find_element(By.XPATH, '//*[@id="tab-year"]/ul/li[' +ntt+ ']').click()
with open('dianYing.csv', 'w', newline='', encoding='utf-8') as f:
    wher = csv.writer(f)
    wher.writerow(['电影名称', '票房', '类型', '产地'])
    for i in data:
        wher.writerow(i)
# # 关闭浏览器
browser.quit()


print("hello")
